/**
 * 文档管理--文档列表的单例对象
 */
var Documents = {
    id: "DocumentsTable",//表格id
    seItem: null,		//选中的第一条记录
    ids: null,//选中的所有记录ids
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Documents.initColumn = function () {
    var columns = [
        {field: 'selectItem'},
        {title: '编号', field: 'id', align: 'center', valign: 'middle', sortable: true},
        {title: '文档名', field: 'document_name', align: 'center', valign: 'middle', sortable: true},
        {title: '文档类型', field: 'format', align: 'center', valign: 'middle', sortable: true},
        {title: '标签', field: 'tag', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'createtime', align: 'center', valign: 'middle', sortable: true},
        {title: '分类', field: 'category_name', align: 'center', valign: 'middle', sortable: true},
        {title: '子分类', field: 'child_category_name', align: 'center', valign: 'middle', sortable: true},
        {title: '是否上架', field: 'is_online', align: 'center', valign: 'middle', sortable: true},
        {title: '阅读数', field: 'read_num', align: 'center', valign: 'middle', sortable: true},
        {title: '下载量', field: 'download_num', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};

/**
 * 检查是否选中
 */
Documents.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Documents.seItem = selected[0];
        var ids = "";
        for (var i = 0; i < selected.length; i++) {
            var item = selected[i];
            ids = ids + item.id + ",";
        }
        ids = ids.substring(0, ids.length - 1);
        Documents.ids = ids;
        return true;
    }
};

/**
 * 点击添加文档
 */
Documents.openDocuments = function () {
    var index = layer.open({
        type: 2,
        title: '新建文档',
        area: ['1000px', '800px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/documents/documents_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看文档详情
 */
Documents.openDocumentsDetail = function () {
    if (this.check()) {
        if (isNaN(Documents.ids)) {
            Feng.info("请不要选择多条记录！");
            return false;
        }
        var index = layer.open({
            type: 2,
            title: '文档详情',
            area: ['1000px', '800px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/documents/documents_update/' + Documents.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 预览
 * @returns {boolean}
 */
Documents.preview = function () {
    if (this.check()) {
        if (isNaN(Documents.ids)) {
            Feng.info("请不要选择多条记录！");
            return false;
        }
        var index = layer.open({
            type: 2,
            title: '网页预览',
            area: ['1000px', '700px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/documents/preview/' + Documents.seItem.id
        });
        this.layerIndex = index;
    }
};
/**
 * 上架
 */
Documents.online = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/documents/online", function (data) {
                Feng.success("上架成功!");
                Documents.table.refresh();
            }, function (data) {
                Feng.error("上架失败!" + data.responseJSON.message + "!");
            });
            ajax.set("documentsIds", Documents.ids);
            ajax.start();
        };
        Feng.confirm("是否确定上架所选文档", operation);
    }
};
/**
 * 下架
 */
Documents.cancelOnline = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/documents/cancel_online", function (data) {
                Feng.success("下架成功!");
                Documents.table.refresh();
            }, function (data) {
                Feng.error("下架失败!" + data.responseJSON.message + "!");
            });
            ajax.set("documentsIds", Documents.ids);
            ajax.start();
        };
        Feng.confirm("是否确定下架所选文档", operation);
    }
};

/**
 * 点击分类ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
Documents.onClickCategory = function (e, treeId, treeNode) {
    var pName = $("#pName").val();
    $("#pName").attr("value", Documents.zTreeInstance.getSelectedVal());
    if (treeNode.id != 0) {
        if (treeNode.pId == 0) {
            $("#categoryPId").attr("value", treeNode.id);
        } else {
            $("#categoryPId").attr("value", treeNode.pId);
            $("#categoryId").attr("value", treeNode.id);
        }
    }
}
/**
 * 显示分类选择的树
 *
 * @returns
 */
Documents.showCategorySelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#categoryMenu").css({
        left: pName.offsetLeft + "px",
        top: 10 + pName.outerHeight() + "px"
    }).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
}
/**
 * 隐藏分类选择的树
 */
Documents.hideCategorySelectTree = function () {
    $("#categoryMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

Documents.search = function () {
    var queryData = {};
    queryData['categoryPId'] = $("#categoryPId").val();
    queryData['categoryId'] = $("#categoryId").val();
    queryData['format'] = $("#format").val();
    queryData['priceType'] = $("#priceType").val();
    queryData['tag'] = $("#tag").val();
    queryData['documentName'] = $("#documentName").val();
    Documents.table.refresh({query: queryData});
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "categoryMenu" || $(
            event.target).parents("#categoryMenu").length > 0)) {
        Documents.hideCategorySelectTree();
    }
}
$(function () {
    var defaultColunms = Documents.initColumn();
    var table = new BSTable(Documents.id, "/documents/list", defaultColunms);
    table.setPaginationType("server");
    Documents.table = table.init();

    var ztree = new $ZTree("categoryMenuTree", "/category/tree_all");
    ztree.bindOnClick(Documents.onClickCategory);
    ztree.init();
    Documents.zTreeInstance = ztree;
});